


// ECE 387 MIDTERM
// Simple code showing a switch's state (ON/OFF) displayed to a local web server
#include <SPI.h>
#include <Ethernet.h>


byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x15, 0xE1 };
IPAddress ip(10, 33, 50, 197); 
EthernetServer server(80);  // create a server at port 80 (default for some reason)

void setup()
{
    Ethernet.begin(mac, ip);    // initialize Ethernet device
    server.begin();   
    pinMode(3, INPUT);  // Sets pin 3 to act as an input
    
}

void loop()
{
    EthernetClient client = server.available();  // Gets a client that is connected to the server and has data available for reading.

        while (client.connected()) { // check to see if client is connected, if so, proceeds
            if (client.available()) {   // client data available to read
                char character = client.read(); // read 1 byte (character) from client
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (character == '\n') {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connnection: close");
                    client.println();
                    // send web page
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>ECE 387 Midterm Switch Example</title>");
                    client.println("<meta http-equiv=\"refresh\" content=\"1\">");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<h1>Switching Check (On/Off)</h1>");
                    client.println("<p>Switch Is Currently:</p>");
                    
                    // if there's an input voltage, print ON, else OFF.
                    if (digitalRead(3)) {
                      client.println("<p>ON</p>");
                    }
                    else {
                      client.println("<p>OFF</p>");
                    }
                    
                    client.println("</body>");
                    client.println("</html>");
                    break;
                }
            }
        } 
        delay(2);      // web browser delay to catch the connection
        client.stop(); // close the connection
}


